package com.woniu.controller.contemtService;

import com.woniu.model.contentMService.entity.Advertisement;
import com.woniu.model.gh.common.Result;
import com.woniu.service.contentService.IAdvertisementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 广告管理表 前端控制器
 * </p>
 *
 * @author 作者
 * @since 2023-10-20
 */
@RestController
@RequestMapping("/advertisement")
public class AdvertisementController {
    @Autowired
    private IAdvertisementService iAdvertisementService;

    @GetMapping("/findById")//查看详情
    public Result findById(Integer id){
        Advertisement id1 = iAdvertisementService.getById(id);
        return Result.ok(id1);
    }

    //删除
    @PostMapping("/deleteById")
    public Result deleteById(@RequestBody Integer id) {
        boolean b = iAdvertisementService.removeById(id);
        return Result.ok(b);
    }

    /**
     * 添加或修改
     */

    @PostMapping("/add")
    public Result save(@RequestBody Advertisement advertisement) {
        boolean save = iAdvertisementService.save(advertisement);
        return Result.ok(save);
    }

    @PostMapping("/update")
    public Result update(@RequestBody Advertisement department){
        iAdvertisementService.save(department);
        return Result.ok();
    }


    /**
     * 批量删除
     */
    @PostMapping("/bath")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        boolean id = iAdvertisementService.removeByIds(ids);
        return Result.ok(id);
    }

    @PostMapping("/changeBoole")
    public Result changeBoole(@RequestBody Advertisement advertisement) {
//        if (advertisement.getOnlineStatus() == null){
//            advertisement.setOnlineStatus(1);
//        }

         if (advertisement.getOnlineStatus() == 0)
            advertisement.setOnlineStatus(1);
        else advertisement.setOnlineStatus(0);
        iAdvertisementService.updateById(advertisement);
        return Result.ok();
    }
}
